package cn.fxyz.page;
import java.util.ArrayList;
import java.util.List;

public class PageBean<T>{
	// T 在运行时，才可以获得具体的类型
	private String path="/everyOrder";
	
	//必选项
	private int pageNum = 1;		//第几页(当前页)
	private int pageSize = 1;		//每页显示个数(固定值)
	private int totalRecord;	//总记录数（查询数据库）--注意条件
	
	//计算项
	private int startIndex;		//开始索引(计算)
	private int totalPage;		//总分页数(计算)
	
	//数据
	private List<T> data=new ArrayList<T>();		//分页数据
	
	//动态显示条
	private int start ;
	private int end ;

	//show方法,支持前端代码的复用
	//	第1/6页 [首页] [上一页] 1   2   3   [下一页]   [尾页]
	public String getShow(){
		StringBuffer sb=new StringBuffer();
		sb.append("第"+pageNum+"/"+Math.ceil((float)totalRecord/pageSize)+"页");
		sb.append("<a href='"+path+"/"+data.get(0).getClass().getSimpleName()+"/findAll.do?pageNum=1'>首页</a>");
		sb.append("<a href='"+path+"/"+data.get(0).getClass().getSimpleName()+"/findAll.do?pageNum="+((pageNum-1)<=0?1:pageNum-1)+"'>上一页</a>");
		sb.append("<a href='"+path+"/"+data.get(0).getClass().getSimpleName()+"/findAll.do?pageNum="+((pageNum+1)>totalPage?totalPage:pageNum+1)+"'>下一页</a>");
		sb.append("<a href='"+path+"/"+data.get(0).getClass().getSimpleName()+"/findAll.do?pageNum="+totalPage+"'>尾页</a>");
		return sb.toString();
	}

	public PageBean() {}
	public PageBean(int pageNum,int totalRecord) {
		this.totalRecord=totalRecord;
		//判断页数是否超出可用范围（即用户是否在浏览器直接输入pageNum属性）
		if(pageNum<=0)  //当参数pageNum《=0时
			this.pageNum=1;
		else{
			this.pageNum=
					pageNum > (int)Math.ceil( (float)totalRecord / pageSize )?(int)Math.ceil( (float)totalRecord / pageSize ):pageNum;
		}
		//计算开始位置
		startIndex=(this.pageNum-1)*this.pageSize;

		//计算总分页数
		this.totalPage = (this.totalRecord + this.pageSize - 1) / this.pageSize;
		
		//动态分页
	}
	
	public PageBean(int pageNum, int pageSize, int totalRecord) {
		super();
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.totalRecord = totalRecord;
		
		// 计算
		//1 开始索引
		this.startIndex = (this.pageNum - 1) * this.pageSize;
		
		//2 总分页数
		this.totalPage = (this.totalRecord + this.pageSize - 1) / this.pageSize;
		
		
		//3 动态显示条
		//3.1 初始化数据 -- 显示10个分页
		this.start = 1;
		this.end = 3;
		
		//3.2 初始数据  ， totalPage = 4
		if(this.totalPage <= 3){
			this.end = this.totalPage;
		} else {
			// totalPage =  22 
			
			//3.3 当前页 前4后5
			this.start = this.pageNum - 1;
			this.end = this.pageNum + 1;
			
			// *  pageNum = 1
			if(this.start < 1){
				this.start = 1;
				this.end = 3;
			}
			
			// * pageNum = 22
			if(this.end > this.totalPage){
				this.end = this.totalPage;
				this.start = this.totalPage - 2;
			}
		}
		
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}
}
